package com.yyx.app1.Data.inconpatibility;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.yyx.app1.Data.DataInit;
import com.yyx.app1.Data.Medication.Medicine;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yuexing on 2015/9/30.
 */
public class TabooCompatible_SQLiteOperation {
    private DataInit dataInit;
    private String TableName;

    public TabooCompatible_SQLiteOperation(DataInit dataInit) {
        this.dataInit = dataInit;
        TableName = dataInit.getDB_TabooCompatibleTableName();
    }

    public void deleteAll()
    {
        SQLiteDatabase db=null;
        try
        {
            db = dataInit.creatOrOpenDatabase();
            db.execSQL("delete from " + TableName + " ;");
//            db.execSQL("update "+TableName+" SET number = 0 ;");
        }
        catch (Exception e)
        {
            Log.e("异常1509301900", e.toString());
        }
        finally
        {
            Log.i("tabooCompatible", "deleteAll");
            if (db!=null)
                db.close();
        }
    }

    public long add(TabooCompatible tabooCompatible) {
        long result=0;
        SQLiteDatabase db=null;
        try
        {
            db = dataInit.creatOrOpenDatabase();
            ContentValues values = new ContentValues();
            values.put("id",tabooCompatible.getId());//private String id;
            values.put("firstMedicine",tabooCompatible.getFirstMedicine());//private String firstMedicine;  //主药品名
            values.put("secondMedicine",tabooCompatible.getSecondMedicine());//private String secondMedicine; //从药品名
            values.put("effect",tabooCompatible.getEffect());//private String effect;		   //作用机制
            values.put("tcproperty",tabooCompatible.getTcproperty());//private String tcproperty;     //配伍属性
            values.put("releaseTime",tabooCompatible.getReleaseTime());//private String releaseTime;    //发布时间
            values.put("adminId",tabooCompatible.getAdminId());//private String adminId;        //管理员id
            values.put("status",tabooCompatible.getStatus());//private Integer status;  	   //状态   0：创建  1：上架   -1：下架
            values.put("onTime",tabooCompatible.getOnTime());//private String onTime;         //上架时间
            values.put("offTime",tabooCompatible.getOffTime());//private String offTime;        //下架时间
            values.put("startTime",tabooCompatible.getStartTime());//private String startTime;
            values.put("endTime",tabooCompatible.getEndTime());//private String endTime;
            result = db.insert(TableName,null,values);
        }
        catch (Exception e)
        {
            Log.e("异常1511091712",e.toString());
        }
        finally
        {
            if (db!=null)
                db.close();
            return result;
        }
    }

    public long add(TabooCompatible tabooCompatible,SQLiteDatabase db) {
        long result=0;
        try
        {
            ContentValues values = new ContentValues();
            values.put("id",tabooCompatible.getId());//private String id;
            values.put("firstMedicine",tabooCompatible.getFirstMedicine());//private String firstMedicine;  //主药品名
            values.put("secondMedicine",tabooCompatible.getSecondMedicine());//private String secondMedicine; //从药品名
            values.put("effect",tabooCompatible.getEffect());//private String effect;		   //作用机制
            values.put("tcproperty",tabooCompatible.getTcproperty());//private String tcproperty;     //配伍属性
            values.put("releaseTime",tabooCompatible.getReleaseTime());//private String releaseTime;    //发布时间
            values.put("adminId",tabooCompatible.getAdminId());//private String adminId;        //管理员id
            values.put("status",tabooCompatible.getStatus());//private Integer status;  	   //状态   0：创建  1：上架   -1：下架
            values.put("onTime",tabooCompatible.getOnTime());//private String onTime;         //上架时间
            values.put("offTime",tabooCompatible.getOffTime());//private String offTime;        //下架时间
            values.put("startTime",tabooCompatible.getStartTime());//private String startTime;
            values.put("endTime",tabooCompatible.getEndTime());//private String endTime;
            result = db.insert(TableName,null,values);
        }
        catch (Exception e)
        {
            Log.e("异常",e.toString());
        }
        finally
        {
            return result;
        }
    }

    public List<Map> fuzzySearch(String name)
    {
        List<Map> list=new ArrayList<>();
        SQLiteDatabase db=null;
        Cursor cursor=null;
        try
        {
            db = dataInit.creatOrOpenDatabase();
            cursor = db.query(TableName, new String[]{"firstMedicine", "secondMedicine"}, "firstMedicine like ? ", new String[]{"%" + name + "%"}, null, null, null, null);
            while (cursor.moveToNext())
            {
                HashMap<String,String> map = new HashMap<>();
                map.put("firstMedicine",cursor.getString(cursor.getColumnIndex("firstMedicine")));
                map.put("secondMedicine",cursor.getString(cursor.getColumnIndex("secondMedicine")));
                list.add(map);
            }
        }
        catch (Exception e)
        {
            Log.e("异常",e.toString());
        }
        finally
        {
            if (cursor!=null)
                cursor.close();
            if (db!=null)
                db.close();
            return list;
        }
    }

    public boolean find(String firstMedicine) {
        boolean result=false;
        SQLiteDatabase db=null;
        try
        {
            db = dataInit.creatOrOpenDatabase();
            Cursor cursor = db.query(TableName, null, "firstMedicine=?", new String[]{firstMedicine}, null, null, null);
            result = cursor.moveToNext();
            if (cursor!=null)
                cursor.close();
        }
        catch (Exception e)
        {
            Log.e("异常1510141439",e.toString());
        }
        finally
        {
            if (db!=null)
                db.close();
            return result;
        }
    }

    public List getDemo_10name() {
        SQLiteDatabase db=null;
        Cursor cursor=null;
        List<String> list=new ArrayList<>();
        try
        {
            db = dataInit.creatOrOpenDatabase();
            cursor = db.query(TableName, new String[]{"distinct firstMedicine"}, null, null, null, null, null);
            while (cursor.moveToNext())
            {
                if (list.size()>14)
                {
                    break;
                }
                list.add(cursor.getString(cursor.getColumnIndex("firstMedicine")));
            }
        }
        catch (Exception e)
        {
            Log.e("异常1510141440",e.toString());
        }
        finally
        {
            if (cursor!=null)
                cursor.close();
            if (db!=null)
                db.close();
            return list;
        }
    }

    public List<Map> getItemByfirstMedicine(String firstMedicineOrsecondMedicine) {
        SQLiteDatabase db=null;
        Cursor cursor=null;
        List<Map> list=new ArrayList<>();
        try
        {
            db = dataInit.creatOrOpenDatabase();
            cursor = db.query(TableName, new String[]{"firstMedicine","secondMedicine"}, "firstMedicine=? ", new String[]{firstMedicineOrsecondMedicine},null,null,null);
            while (cursor.moveToNext())
            {
                HashMap<String,String> map = new HashMap<>();
                map.put("firstMedicine",cursor.getString(cursor.getColumnIndex("firstMedicine")));
                map.put("secondMedicine",cursor.getString(cursor.getColumnIndex("secondMedicine")));
                list.add(map);
            }
        }
        catch (Exception e)
        {
            Log.e("异常",e.toString());
        }
        finally
        {
            if (cursor!=null)
                cursor.close();
            if (db!=null)
                db.close();
            return list;
        }
    }

    public void InjectionSQList()
    {
        SQLiteDatabase db = dataInit.creatOrOpenDatabase();
        File file=new File(dataInit.getDataBaseDirectory()+"/"+dataInit.getTabooCompatibleDataFileName());
        try
        {
            BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
            String strline;
            while ((strline=bufferedReader.readLine())!=null)
            {
                JSONObject jsonObject=new JSONObject(strline);
                TabooCompatible tabooCompatible = new TabooCompatible();
                tabooCompatible.setId(jsonObject.getString("id"));//private String id;
                tabooCompatible.setFirstMedicine(jsonObject.getString("firstMedicine"));//private String firstMedicine;  //主药品名
                tabooCompatible.setSecondMedicine(jsonObject.getString("secondMedicine"));//private String secondMedicine; //从药品名
                tabooCompatible.setEffect(jsonObject.getString("effect"));//private String effect;		   //作用机制
                tabooCompatible.setTcproperty(jsonObject.getString("tcproperty"));//private String tcproperty;     //配伍属性
                tabooCompatible.setReleaseTime(jsonObject.getString("releaseTime"));//private String releaseTime;    //发布时间
                tabooCompatible.setAdminId(jsonObject.getString("adminId"));//private String adminId;        //管理员id
                tabooCompatible.setStatus(jsonObject.getInt("status"));//private Integer status;  	   //状态   0：创建  1：上架   -1：下架
                tabooCompatible.setOnTime(jsonObject.getString("onTime"));//private String onTime;         //上架时间
                tabooCompatible.setOffTime(jsonObject.getString("offTime"));//private String offTime;        //下架时间
                tabooCompatible.setStartTime(jsonObject.getString("startTime"));//private String startTime;
                tabooCompatible.setEndTime(jsonObject.getString("endTime"));//private String endTime;
                add(tabooCompatible, db);
            }
            Log.i("tabooCompatible", "结束");
            bufferedReader.close();
        }
        catch (Exception e)
        {
            Log.e("异常", e.toString());
        }
        finally
        {
            if (db!=null)
                db.close();
        }
    }
}
